/**
 * Progetto PRAISED.
 * 26/gen/2011
 */
package it.uniroma3.dia.praised.core.facade;

import it.uniroma3.dia.praised.core.dictionary.Dictionary;
import it.uniroma3.dia.praised.core.exception.DictionaryException;
import it.uniroma3.dia.praised.core.exception.PersistenceException;
import it.uniroma3.dia.praised.core.model.CandidateProteinName;
import it.uniroma3.dia.praised.core.model.InputText;
import it.uniroma3.dia.praised.core.model.ProteinName;
import it.uniroma3.dia.praised.core.model.Tokenizer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;

/**
 * @author Accosta Augusto
 * @author Lopez Elisabetta
 *
 */
public class DictionaryFacade {

	public static void retrieveAndDiscoverProtein(String plainText, List<ProteinName> proteinList) {
		try {
			InputText inputText = Tokenizer.tokenize(plainText);
			retrieveAndDiscoverProtein(inputText, proteinList);
			
		} catch (PersistenceException e) {
			e.printStackTrace();
		} catch (DictionaryException e) {
			e.printStackTrace();
		}
	}
	
	public static void retrieveAndDiscoverProtein(InputText inputText, List<ProteinName> proteinList) throws PersistenceException, DictionaryException {
		Dictionary dictionary = new Dictionary(proteinList);
		List<CandidateProteinName> candidateList = dictionary.discoverProtein(inputText);
		try {
			IOUtils.writeLines(candidateList, null, new FileOutputStream(new File(inputText.getOriginalTextInput().replace("/", " ") + ".txt")), "UTF-8");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
